import {
	find
} from "../../api/modules/find";

export const getDicFromApi = (key) => find.groupResource.dic().then((data) => {
	try {
		uni.setStorageSync('dic', data);
	} catch (e) {
		// error
	}
	return key ? data[key] : data
})

export const getDicItem = (key) => {
	return new Promise((resolve, reject) => {
		const dicAll = uni.getStorageSync('dic')
		if (dicAll) {
			resolve(key ? dicAll[key] : dicAll)
		} else {
			getDicFromApi(key).then((data) => {
				resolve(data)
			})
		}
	})
}

export const getDicItemMap = (key) => {
	return new Promise((resolve, reject) => {
		getDicItem(key).then((data) => {
			const dataMap = data.reduce((init, item) => {
				init[item.code]= item.name
				return init
			}, {})
			resolve(dataMap)
		})
	})
}